$(function() {
	/**
	 * 取得评论列表
	 */
	function getComment(page) {
		$.post(urlComment, {
				'user_id':userId,
				'num_iid':numIid,
				'page':page
			}, function(data) {
				var commmentArr=data.split('@');
				var biaochi = parseInt(commmentArr[1]/5*380);
				$("#biaochi").css("margin-left",biaochi+"px");
				$(".ajaxpjdf").html(commmentArr[1]);
				$("#pjdfnum").html(commmentArr[2]);
				$("#itemBasic .comment b").html(commmentArr[2]);
				$("#comment").fadeOut().html(commmentArr[4]).fadeIn();
				$(".page").html(commmentArr[5]);
				$("#page_nav a").unbind().click(function() {
					var page = $(this).attr("href");
					///
					getComment(page);
					return false;
				});
		});
	}
	
	/**
	 * 切换TAB
	 */
	function tabChange(tab) {
		$(".con").hide();
		$(".cmenu li").removeClass("this");
		$("#con_"+$("#"+tab).attr("id").substr(4, 1)).show();
		$("#"+tab).addClass("this");
	}
	
	///
	$(".cmenu li").click(function() {tabChange($(this).attr("id"));});
	
	///
	getTaobaoke(numIid, function(list) {
		if (list.length > 0) {
			var volume = list[0].volume;
			var commission = list[0].commission*81/100;
			$("#itemBasic .volume b").html(volume);
			$("#itemBasic .commission b").html("￥"+ commission.toFixed(2));
		}
	});
	
	///
	getComment(1);
	
	///
	$("#con_1 img").lazyload({
		effect : "fadeIn"
	});
	
	/// 登录/注册面板
	var isIE = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent);
	
	/**
	 * 
	 */
	function showWindow() {
		var arrPageScroll = $.getPageScroll();
		var arrPageSizes = $.getPageSize();
		var width = $("#userPannel").width();
		var height = $("#userPannel").height();
		var margintop = 0 - parseInt((height / 2), 10);
		var marginleft = 0 - parseInt((width / 2), 10);
		
		if (isIE) {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow", "hidden");
			margintop += arrPageScroll[1];
		}
		
		///
		$("#popOverlay").css("height", parseInt(arrPageSizes[1])).show();
		$("#userPannel").show().css({
			marginLeft : marginleft + 'px',
			marginTop : margintop + 'px'
		});
	}
	
	/**
	 * 
	 */
	function hideWindow() {
		$("#popOverlay").hide();
		$("#userPannel").hide();
		if (isIE) {
			$("body", "html").css({
				height : "auto",
				width : "auto"
			});
			$("html").css("overflow", "");
		}
	}
	
	if (loginState == "") {
		/// 
		$("#userPannel .tab li").click(function() {
			$("#userPannel .tab li").removeClass("on");
			$(this).addClass("on");
			
			$(".pannel").hide();
			$("#userPannel_"+$(this).attr("id").substr(8, 1)).show();
		});
		
		///
		$("#userPannel .close").click(function() {
			hideWindow();
		});
		
		///
		$("#btnBuy").click(function() {
			showWindow();
			return false;
		});
		
		///
		document.onkeydown = function(e) {
			if (e == null) { // ie
				keycode = event.keyCode;
			} else { // mozilla
				keycode = e.which;
			}
			if (keycode == 27) {
				hideWindow(); // 清除
			}
		};
		
		/// 登录表单
		$("#formLogin").validate({
			rules: {
				username: {
					required: true,
					email: true
				},
				password: {
					required: true
				}
			},
			messages: {
				username: {
					required: "请输入用户名",
					email: "请输入合法邮箱"
				},
				password: {
					required: "请输入密码"
				}
			},
			submitHandler: function() {
				form.submit();
				///
				return false;
			}
		});
		
		/// 注册表单
		$("#formRegister").validate({
			rules: {
				username: {
					required: true,
					email: true,
					maxByte: 100
				},
				password: {
					required: true,
					minlength: 6,
					maxlength: 10
				},
				password2: {
					required: true,
					minlength: 6,
					maxlength: 10,
					equalTo: "#formRegister input[name='password']"
				},
				captcha: "required"
			},
			messages: {
				username: {
					required: "请输入用户名",
					email: "请输入合法邮箱",
					maxByte: "请输入合法邮箱"
				},
				password: {
					required: "请输入密码",
					minlength: "密码至少要6位",
					maxlength: "密码不能超过10位"
				},
				password2: {
					required: "请输入确认密码",
					minlength: 	"确认密码至少要6位",
					maxlength: "确认密码不能超过10位",
					equalTo:"两次输入的密码不一致"
				},
				captcha: "请输入验证码"
			},
			errorPlacement: function(error, element) {
				error.appendTo(element.parent("div"));
			},
			submitHandler: function() {
				form.submit();
				///
				return false;
			}
		});
		
		///
		$("#captchaRef").click(function() {
			$("#captchaImg").attr("src", urlCaptcha + Math.random());
		});
	}
});
